package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.CollectionsType;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.CollectionsTypeMapper;
import com.example.springboot.service.ICollectionsTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CollectionsTypeService implements ICollectionsTypeService {

    @Autowired //通过autowired导入collectionsTypeMapper
    CollectionsTypeMapper collectionsTypeMapper;

    public List<CollectionsType> list(){
        return collectionsTypeMapper.list();
    }

    @Override
    public PageInfo<CollectionsType> page(BaseRequest baseRequest){
//        dependency引入pagehelper
//        PageHelper传入页面和每页信息条数
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<CollectionsType> collectionsTypes=collectionsTypeMapper.listByCondition(baseRequest);
        return new PageInfo<>(collectionsTypes);
    }

    @Override
    public void save(CollectionsType collectionsType){//        当做卡号来处理
        Date date= new Date();
//     通过hashCode编码将fastSimpleUUID产生的字母和数字转为数字
        try{
            collectionsTypeMapper.save(collectionsType);
        }catch(DuplicateKeyException e){
            log.error("数据插入失败,产品备注信息:{}",collectionsType.getRemark());
            throw new ServiceException("用户插入失败");
        }
    }


    @Override
    public CollectionsType getById(Integer id) {
        return collectionsTypeMapper.getById(id);
    }

    @Override
    public void update(CollectionsType collectionsType) {
        collectionsType.setUpdateTime(new Date());
        collectionsTypeMapper.updateById(collectionsType);

    }

    @Override
    public void deleteById(Integer id) {
        collectionsTypeMapper.deleteById(id);
    }
    
}
